home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / stat / stat.c < prev    next >
C/C++ Source or Header  |  1990-12-03  |  6KB  |  259 lines

  1. /* 
  2.  * stat.c --
  3.  *
  4.  *    Get the complete attributes of a file
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/cmds/stat/RCS/stat.c,v 1.4 89/01/29 16:09:07 rab Exp Locker: shirriff $";
  18. #endif /* not lint */
  19.  
  20. #include <sprite.h>
  21. #include <fs.h>
  22. #include <stdio.h>
  23. #include <option.h>
  24. #include <sys/types.h>
  25. #include <time.h>
  26.  
  27. /*
  28.  * Default values for parameters
  29.  */
  30. Boolean shortForm = FALSE;
  31. Boolean link = FALSE;
  32. Boolean showDay = FALSE;
  33. Boolean timing = FALSE;
  34. int    numTimes = 1000;
  35.  
  36. Option optionArray[] = {
  37.     OPT_TRUE, "s", (Address)&shortForm, "Output a short form of the attributes",
  38.     OPT_TRUE, "l", (Address)&link, "Get attributes of the link, not the target",
  39.     OPT_TRUE, "d", (Address)&showDay, "Include day of the week in dates",
  40.     OPT_TRUE, "t", (Address)&timing, "Time N Fs_GetAttributes calls",
  41.     OPT_INT,  "n", (Address)&numTimes, "Number of repititions (with -t)",
  42. };
  43. int numOptions = sizeof(optionArray) / sizeof(Option);
  44.  
  45. int localOffset;
  46. char * PrintDate();
  47.  
  48. /*
  49.  * main --
  50.  *    Collect arguments, then call Stat() for each file.
  51.  */
  52. void
  53. main(argc, argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     register ReturnStatus status;
  58.     register int i;
  59.     register char *fileName;
  60.  
  61.     argc = Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  62.  
  63.     if (argc < 2) {
  64.     fprintf(stderr, "usage: %s filename [filename ...]\n", 
  65.             argv[0]);
  66.     exit(0);
  67.     }
  68.     Sys_GetTimeOfDay(NULL, &localOffset, NULL);
  69.     localOffset = (localOffset * 60) + 3600;
  70.  
  71.     if (!timing) {
  72.     for (i=1 ; i<argc ; i++) {
  73.         fileName = argv[i];
  74.  
  75.         status = Stat(fileName, link);
  76.         if (status != SUCCESS) {
  77.         fprintf(stderr, "%s: ", fileName);
  78.         Stat_PrintMsg(status, "");
  79.         }
  80.     }
  81.     } else {
  82.     Time before, after;
  83.     Fs_Attributes attrs;
  84.  
  85.     Sys_GetTimeOfDay(&before, NULL, NULL);
  86.     for (i=0 ; i<numTimes ; i++) {
  87.         status = Fs_GetAttributes(argv[1], link, &attrs);
  88.         if (status != SUCCESS) {
  89.         fprintf(stderr, "%s: ", argv[1]);
  90.         Stat_PrintMsg(status, "");
  91.         break;
  92.         }
  93.     }
  94.     Sys_GetTimeOfDay(&after, NULL, NULL);
  95.     if (i > 0) {
  96.         Time_Subtract(after, before, &after);
  97.         printf("%d Fs_GetAttributes of %s at %dus each\n", i, argv[1],
  98.         (after.seconds * 1000000 + after.microseconds) / i);
  99.     }
  100.     }
  101.     exit(status);
  102. }
  103.  
  104. /*
  105.  * Stat --
  106.  *    Get the attributes of a file and print them.
  107.  *
  108.  * Results:
  109.  *    The status code from Fs_GetAttributes
  110.  *
  111.  * Side Effects:
  112.  *    None.
  113.  */
  114. ReturnStatus
  115. Stat(fileName, link)
  116.     char *fileName;    /* File to get the attributes of */
  117.     Boolean link;    /* If TRUE the attributes of a link file are printed,
  118.              * otherwise the attributes of the target of the link 
  119.              * are printed */
  120. {
  121.     ReturnStatus status;
  122.     Fs_Attributes attrs;
  123.  
  124.     status = Fs_GetAttributes(fileName, link, &attrs);
  125.     if (status != SUCCESS) {
  126.     return(status);
  127.     }
  128.  
  129.     PrintFileType(attrs.type);
  130.     PrintFilePermissions(attrs.permissions);
  131.     printf("%2d  ID=(%d,%d) ", attrs.numLinks, attrs.uid, attrs.gid);
  132.     if (shortForm) {
  133.     /*
  134.      * Print a 1-line summary of the attributes.
  135.      */
  136.     if (attrs.type == FS_DEVICE) {
  137.         printf("%5d%5d ", attrs.devType, attrs.devUnit);
  138.     } else {
  139.         printf("%10d ", attrs.size);
  140.     }
  141.     printf("%s %s\n", PrintDate(attrs.dataModifyTime), fileName);
  142.     } else {
  143.     /*
  144.      * Print the complete attributes.
  145.      */
  146.     printf("%7d bytes  %s\n", attrs.size, fileName);
  147.  
  148.     printf("%7s %7s %10s", "Server", "Domain", "File #");
  149.     if (attrs.type == FS_DEVICE) {
  150.         printf("  Device: %7s %7s %7s", "Server", "Type", "Unit");
  151.     }
  152.     printf("\n");
  153.  
  154.     printf("%7d %7d %10d", 
  155.             attrs.serverID, attrs.domain, attrs.fileNumber);
  156.     if (attrs.type == FS_DEVICE) {
  157.         printf("          %7d %7d %7d", 
  158.         attrs.devServerID, attrs.devType, attrs.devUnit);
  159.     }
  160.     printf("\n");
  161.  
  162.     printf("Version %d    UserType 0x%x\n",
  163.         attrs.version, attrs.userType);
  164.  
  165.     printf("Created:         %s\n", PrintDate(attrs.createTime));
  166.     printf("Data modified:   %s\n", PrintDate(attrs.dataModifyTime));
  167.     printf("Descr. modified: %s\n", PrintDate(attrs.descModifyTime));
  168.     printf("Last accessed:   %s\n", PrintDate(attrs.accessTime));
  169.     }
  170.     return(SUCCESS);
  171. }
  172.  
  173. /*
  174.  * PrintFileType --
  175.  *    Print a character to represent a file's type.
  176.  */
  177. PrintFileType(type)
  178.     int type;
  179. {
  180.     char c;
  181.     switch(type) {
  182.     case FS_FILE:
  183.         c = '-';
  184.         break;
  185.     case FS_DIRECTORY:
  186.         c = 'd';
  187.         break;
  188.     case FS_SYMBOLIC_LINK:
  189.         c = 'l';
  190.         break;
  191.     case FS_REMOTE_LINK:
  192.         c = 'r';
  193.         break;
  194.     case FS_DEVICE:
  195.     case FS_REMOTE_DEVICE:
  196.         c = 'D';
  197.         break;
  198.     case FS_NAMED_PIPE:
  199.         c = 'p';
  200.         break;
  201.     case FS_PSEUDO_DEV:
  202.         c = 'x';
  203.         break;
  204.     default:
  205.         printf("(%d)", type);
  206.         return;
  207.     }
  208.     printf("%c", c);
  209. }
  210. /*
  211.  * PrintFilePermissions --
  212.  *    Print out characters to represent the permission bits of a file.
  213.  */
  214. PrintFilePermissions(permissions)
  215.     int permissions;
  216. {
  217.     char c;
  218.     register int i;
  219.     register int bits;
  220.  
  221.     if (permissions & FS_SET_UID) {
  222.     printf("u");
  223.     } else if (permissions & FS_SET_GID) {
  224.     printf("g");
  225.     } else {
  226.     printf("-");
  227.     }
  228.     for (i=0 ; i<3 ; i++) {
  229.     bits = (permissions >> ((2-i)*3)) & 0x7;
  230.     if (bits & FS_WORLD_READ) { c = 'r'; } else { c = '-'; }
  231.     printf("%c", c);
  232.     if (bits & FS_WORLD_WRITE) { c = 'w'; } else { c = '-'; }
  233.     printf("%c", c);
  234.     if (bits & FS_WORLD_EXEC) { c = 'x'; } else { c = '-'; }
  235.     printf("%c", c);
  236.     }
  237. }
  238. /*
  239.  * PrintDate --
  240.  *    Print a date in ascii format
  241.  */
  242.  
  243. char *
  244. PrintDate(time)
  245.     Time time;
  246. {
  247.     time_t t;
  248.     char *string;
  249.  
  250.     t = time.seconds;
  251.     string = asctime(localtime(&t));        
  252.     string[strlen(string) - 1] = '\0';      /* remove trailing '\n' */
  253.     if (showDay) {
  254.     return(string);
  255.     } else {
  256.     return(&string[4]);
  257.     }
  258. }
  259.